package com.longcai.piaojia.adapter;

import android.app.Activity;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.longcai.piaojia.R;
import com.longcai.piaojia.conn.MineCouponListGet;
import com.zcx.helper.bound.BoundView;
import com.zcx.helper.bound.BoundViewHelper;
import com.zcx.helper.scale.ScaleScreenHelperFactory;

import java.util.List;

/**
 * Created by Administrator on 2016/8/4.
 */
public class CouponListAdapter extends ArrayAdapter<MineCouponListGet.MineCouponEntity> {

    private Context context;
    private LayoutInflater layoutInflater;

    private int couponTag;

    public CouponListAdapter(Context context, List<MineCouponListGet.MineCouponEntity> objects, int i) {
        super(context, 0, objects);

        this.context = context;

        couponTag = i;

        layoutInflater = ((Activity) context).getLayoutInflater();
    }


    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {

        ViewHolder viewHolder = null;

        MineCouponListGet.MineCouponEntity mineCouponEntity = getItem(position);

        if (convertView == null) {

            convertView = BoundViewHelper.boundView(viewHolder = new ViewHolder(), ScaleScreenHelperFactory.getInstance().loadView((ViewGroup) layoutInflater.inflate(R.layout.item_coupon_list, null)));

            switch (couponTag) {

                case 1:
                    viewHolder.item_coupon_background_iv.setImageResource(R.mipmap.coupon_notused);
                    break;
                case 2:
                    viewHolder.item_coupon_background_iv.setImageResource(R.mipmap.coupon_expried);
                    break;
                case 3:
                    viewHolder.item_coupon_background_iv.setImageResource(R.mipmap.coupon_used);
                    break;
            }

            convertView.setTag(viewHolder);

        } else {

            viewHolder = (ViewHolder) convertView.getTag();

        }

        viewHolder.item_coupon_list_money_tv.setText((int)(Float.valueOf(mineCouponEntity.money) * 100) / 100 + "");
        viewHolder.item_coupon_list_title_tv.setText(mineCouponEntity.title);
        viewHolder.item_coupon_list_condition_tv.setText(context.getString(R.string.coupon_list_condition_front_text) + (int) (Float.valueOf(mineCouponEntity.total) * 100) / 100 + context.getString(R.string.coupon_list_condition_back_text));
        viewHolder.item_coupon_list_use_date_tv.setText(context.getString(R.string.coupon_list_condition_use_date_text) + mineCouponEntity.starttime + context.getString(R.string.space) + context.getString(R.string.goods_info_symbols_text) + context.getString(R.string.space) + mineCouponEntity.endtime);

        return convertView;

    }

    private class ViewHolder {

        @BoundView(R.id.item_coupon_background_iv) private ImageView item_coupon_background_iv;

        @BoundView(R.id.item_coupon_list_money_tv) private TextView item_coupon_list_money_tv;
        @BoundView(R.id.item_coupon_list_title_tv) private TextView item_coupon_list_title_tv;
        @BoundView(R.id.item_coupon_list_condition_tv) private TextView item_coupon_list_condition_tv;
        @BoundView(R.id.item_coupon_list_use_date_tv) private TextView item_coupon_list_use_date_tv;

    }

}
